// pages/house/house.js
var QQMapWX = require('../../qqmap-wx-jssdk.min.js');
 
// 实例化API核心类
var qqmapsdk = new QQMapWX({
    key: '6FTBZ-OPECR-OYVWT-WULAV-V7VPQ-FAFCT' // 必填
});


Page({

  data: {
    userIdForRental:"",
    rentalid:"",
    house_pic:[],
    house_name:'',
    address:'',
    rentmoney:'',
    type:'',
    area:'',
    see:'',
    intro:'',
    size:[['1室','2室','3室','4室','5室'],
          ['0厅','1厅','2厅','3厅'],
          ['0卫','1卫','2卫','3卫']],
    device:[
      {
        urly:'/pic/电视有.png',
        urln:'/pic/电视.png',
        name:'电视机',
        choose:true
      },{
        urly:'/pic/微波炉有.png',
        urln:'/pic/微波炉.png',
        name:'微波炉',
        choose:false
      },{
        urly:'/pic/暖气有.png',
        urln:'/pic/暖气.png',
        name:'暖气',
        choose:false
      },{
        urly:'/pic/洗衣机有.png',
        urln:'/pic/洗衣机.png',
        name:'洗衣机',
        choose:false
      },{
        urly:'/pic/浴缸有.png',
        urln:'/pic/浴缸.png',
        name:'浴缸',
        choose:false
      },{
        urly:'/pic/阳台有.png',
        urln:'/pic/阳台.png',
        name:'阳台',
        choose:false
      },{
        urly:'/pic/空调有.png',
        urln:'/pic/空调.png',
        name:'空调',
        choose:false
      },{
        urly:'/pic/衣柜有.png',
        urln:'/pic/衣柜.png',
        name:'衣柜',
        choose:false
      },{
        urly:'/pic/冰箱有.png',
        urln:'/pic/冰箱.png',
        name:'冰箱',
        choose:false
      },{
        urly:'/pic/油烟机有.png',
        urln:'/pic/油烟机.png',
        name:'热水器',
        choose:false
      },{
        urly:'/pic/WIFI有.png',
        urln:'/pic/WIFI.png',
        name:'WIFI',
        choose:false
      },{
        urly:'/pic/水龙头有.png',
        urln:'/pic/水龙头.png',
        name:'水龙头',
        choose:false
      },

    ],
    comment:[],
    personaltarget:'none',
    collect:'',
    content:'',
    wxAva:"",
    nickname:'',
    userName:"",
    contactPhone:"",

  },
  onLoad(options){
    var that = this;
    // console.log(options.rentalid)
    this.setData({
      rentalid:options.rentalid
    })

    //渲染租房信息
    wx.request({
      url: 'http://127.0.0.1:8080/rental/rentalById',
      method:"POST",
      data:{rentalid:options.rentalid},
      success(res){
        // console.log(res.data[0])
        //设备渲染
        var device = res.data[0].deviceInfo.split(',');
        for(var i=0;i<device.length;i++){
          var save = 'device['+i+'].choose'
          that.setData({
            [save]:true
          })
        }
        //房型渲染
        var data = res.data[0];
          var index = JSON.parse(data.size);
          var type = that.data.size[0][index[0]] + 
                    that.data.size[1][index[1]] + 
                    that.data.size[2][index[2]];
          
        that.setData({
          userIdForRental:data.userIdForRental,
          house_pic:JSON.parse(data.rentalPic),
          house_name:data.rentalTitle,
          address:data.rentalAddress,
          rentmoney:data.monthRent,
          type:type,
          see:data.rentalSee,
          intro:data.rentalIntro,
          area:data.area,
          wxAva:data.wxAva,
          nickname:data.nickName,
          userName:data.userName,
          contactPhone:data.phone,

        })

        var rentalsee = res.data[0].rentalSee + 1;
        // console.log(rentalsee)
        
        //增加浏览量
        wx.request({
          url: 'http://127.0.0.1:8080/rental/updateSee',
          method:"POST",
          data:{
            rentalid:options.rentalid,
            rentalsee:rentalsee
          },
          success(res){
            if(res.data){
              that.setData({
                see:rentalsee
              })
            }
            // console.log(res)
          }
        })
      }
    })

    //查询是否收藏
    wx.request({
      url: 'http://127.0.0.1:8080/rentalCollect/rentalCollectById',
      method:"POST",
      data:{
        userid:wx.getStorageSync('openid'),
        rentalid:options.rentalid
      },
      success(res){
        if(res.data.length != 0){
          that.setData({
            collect:true
          })
        }else{
          that.setData({
            collect:false
          })
        }
       
      }
    })



  },
  onShow(){
    var that = this;

    //查找该租房信息评论
    wx.request({
      url: 'http://127.0.0.1:8080/rentalJudge/rentalJudge',
      method:"POST",
      data:{rentalid:that.data.rentalid},
      success(res){
        // console.log(res.data)
        that.setData({
          comment:res.data
        })
      }
    })
  },

/*点击地址 */
  //展示地址位置
  showaddress(e){
    var _this = this;
    //调用地址解析接口
    qqmapsdk.geocoder({
      //获取表单传入地址
      address: _this.data.address, //地址参数，例：固定地址，address: '北京市海淀区彩和坊路海淀西大街74号'
      success: function(res) {//成功后的回调
        console.log(res);
        var res = res.result;
        var latitude = res.location.lat;
        var longitude = res.location.lng;
        //根据地址解析在地图上标记解析地址位置
        _this.setData({ // 获取返回结果，放到markers及poi中，并在地图展示
          markers: [{
            id: 0,
            title: res.title,
            latitude: latitude,
            longitude: longitude,
            iconPath: '/pic/地标.png',//图标路径
            width: 30,
            height: 30,
            // callout: { //可根据需求是否展示经纬度
            //   content: latitude + ',' + longitude,
            //   color: '#000',
            //   display: 'ALWAYS'
            // }
          }],
          poi: { //根据自己data数据设置相应的地图中心坐标变量名称
            latitude: latitude,
            longitude: longitude
          },
        });

        wx.openLocation({
          latitude:latitude,
          longitude:longitude,
          address: _this.data.address,
          name: _this.data.address,
          scale: 28,
        })
      },
      fail: function(error) {
        console.error(error);
      },
      complete: function(res) {
        console.log(res);
      }
    })
  },
/*获得评价*/
  getcomment(e){
    this.setData({
      content:e.detail.value
    })
  },
  comment(){
    if(wx.getStorageSync('openid') != ""){
      if(this.data.content){
        var that = this;
        var nowtime = new Date().toJSON().substring(0,10)+' '+new Date().toTimeString().substring(0,8);
        var data = {
          judgetext:this.data.content,
          judgetime:nowtime,
          userid:wx.getStorageSync('openid'),
          rentalid:this.data.rentalid
        }
        // console.log(data)
        //发布评论
        wx.request({
          url: 'http://127.0.0.1:8080/rentalJudge/addRentalJudge',
          method:"POST",
          data:data,
          success(res){
            // console.log(res)
            if(res.data){
              that.onShow();
              that.setData({
                content:""
              })
            }
          }
        })
      }
    }else{
      wx.showToast({
        title: '请先登录',
        duration: 1000,
        icon: "error",
        success: (res) => {},
      })
    }
   
   
  },

/* 展示发送信息人的信息*/
  showperson(e){
    if(wx.getStorageSync('isuser')  == "yes"){
      this.setData({
            personaltarget:false
          })
    }else{
      wx.showToast({
        title: '身份未通过审核',
        duration: 1000,
        icon: "error",
        success: (res) => {},
      })
    }
   
  },
  cancelperson(){
    this.setData({
      personaltarget:'none'
    })
  },

  /*收藏 */
  collect(e){
    if(wx.getStorageSync('openid') != ""){
       var that = this
    // console.log(this.data.collect)
    var collect = this.data.collect;
    //当前已收藏
    if(collect){
      // 删除该收藏
      wx.request({
        url: 'http://127.0.0.1:8080/rentalCollect/delRentalCollect',
        method:"POST",
        data:{
          rentalid:that.data.rentalid,
          userid:wx.getStorageSync('openid')
        },
        success(res){
          if(res.data){
            that.setData({
              collect:false
            })
          }
          // console.log(res)
        }
      })
    }else{
      //点击收藏
      var data = {
        nowtime:new Date().toJSON().substring(0,10)+' '+new Date().toTimeString().substring(0,8),
        userid:wx.getStorageSync('openid'),
        rentalid:this.data.rentalid
      }
      wx.request({
        url: 'http://127.0.0.1:8080/rentalCollect/addRentalCollect',
        method:"POST",
        data:data,
        success(res){
          if(res.data){
            that.setData({
              collect:true
            })
          }
          // console.log(res)
        }
      })
    }
    }else{
      wx.showToast({
        title: '请先登录',
        duration: 1000,
        icon: "error",
        success: (res) => {},
      })
    }
   
  }



})